<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/20
 * Time: 10:33
 */

namespace app\controller\index\video;


use app\common\lib\Common;
use app\common\lib\Puppeteer;
use app\controller\index\BaseController;
use app\exception\IndexException;
use app\logic\index\video\VideoLogic;
use Illuminate\Support\ItemNotFoundException;
use support\Cache;
use support\Request;
use support\Response;

class IndexController extends BaseController
{
    public function asyncUploadVideo(Request $request):Response
    {
        $param = [];
        $param['file'] = $request->file('file','');

        if($request->file('file')->isValid()==false){
            throw new IndexException('上传失败');
        }

        $param['ext'] = $request->file('file')->getUploadExtension();

        if(in_array(strtolower($param['ext']),['mp4','avi','mov','wav'])==false){
            throw new IndexException('请上传视频');
        }

        $name = substr(md5(round(microtime(true)*1000).uniqid()),-20);
        $filePath = runtime_path('ivld/'.date('ymd').'/'.$name.'.'.$param['ext']);
        Common::mkdirs(dirname($filePath));
        $res = copy($request->file('file')->getRealPath(),$filePath);
        if($res==false){
            throw new IndexException(4020,'上传失败');
        }
        unlink($request->file('file')->getRealPath());
        $uploadId = round(microtime(true)*1000).mt_rand(100000,999999);

        // 队列名
        $queue = 'asyncUploadVideo';
        // 数据，可以直接传数组，无需序列化
        $data = ['uploadid'=>$uploadId,'filepath'=>$filePath];
        // 投递消息
        \Webman\RedisQueue\Redis::send($queue, $data);

        return $this->endSuccess(['uploadid'=>$uploadId]);
    }

    public function getUploadVideoProgress(Request $request):Response
    {
        $progress = Cache::get('uploadfile_progress_'.$request->input('uploadid',''),[]);
        if(empty($progress)){
            $n = 1;
            while (true){
                usleep(100000);
                $progress = Cache::get('uploadfile_progress_'.$request->input('uploadid',''),[]);
                if($progress || $n>4){
                    break;
                }
                $n++;
            }

            if(empty($progress)){
                throw new IndexException(4020,'上传uploadid不存在');
            }
        }

        if($progress['progress']<0){
            throw new IndexException(4020,$progress['msg']);
        }

        return $this->endSuccess($progress);
    }

    public function asyncVideoIvld(Request $request):Response
    {
        $param = [];
        $param['file'] = $request->file('file','');

        if($request->file('file')->isValid()==false){
            throw new IndexException('上传失败');
        }

        $param['ext'] = $request->file('file')->getUploadExtension();

        if(in_array(strtolower($param['ext']),['mp4','avi','mov','wav'])==false){
            throw new IndexException('请上传视频');
        }

        $result = VideoLogic::getInstance()->asyncVideoIvld($param);

        return $this->endSuccess($result);
    }

    public function asyncLinkVideoIvld(Request $request):Response
    {
        $param = [];
        $param['video_link_text'] = $request->input('video_link_text','');

        if(empty($param['video_link_text'])){
            throw new IndexException('内容不能为空');
        }

        $result = VideoLogic::getInstance()->asyncLinkVideoIvld($param);

        return $this->endSuccess($result);
    }

    public function getAsyncVideoIvldProgress(Request $request):Response
    {
        $param['requestid'] = $request->input('requestid');
        if(empty($param['requestid'])){
            throw new IndexException(4020,'请求ID不存在');
        }

        $result = VideoLogic::getInstance()->getAsyncVideoIvldProgress($param);

        return $this->endSuccess($result);
    }

    public function submitData(Request $request):Response
    {
        $param = [];
        $param['content'] = $request->input('content');
        $param['video_url'] = $request->input('video_url');
        $param['modelid'] = $request->input('modelid');
        $param['templateid'] = $request->input('templateid');
        $param['musicid'] = $request->input('musicid');
        $param['timbreid'] = $request->input('timbreid');
        $param['music_volume'] = $request->input('music_volume');
        $param['timbre_volume'] = $request->input('timbre_volume');
        $param['speak_speed'] = $request->input('speak_speed');
        $param['fontid'] = $request->input('fontid');
        $param['video_rate'] = $request->input('video_rate');
        if(empty($param['content']) && empty($param['video_url'])){
            throw new IndexException(4020,'视频不存在');
        }
        if(empty($param['templateid'])){
            throw new IndexException(4020,'模型风格必选');
        }

        $data = VideoLogic::getInstance()->submitData($param);

        return $this->endSuccess($data);
    }

    public function getVideoAnalyzeResult(Request $request):Response
    {
        $param = [];
        $param['id'] = $request->input('id');
        if(empty($param['id'])){
            throw new IndexException(4020,'视频不存在');
        }

        $data = VideoLogic::getInstance()->getVideoAnalyzeResult($param);

        return $this->endSuccess($data);
    }
}